package com.cf.swagger.controller;

import com.cf.swagger.entity.User;
import com.cf.swagger.service.UserService;
import com.cf.swagger.until.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Author: YangF
 * @Date: 2020-12-24 15:42
 * @description:
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping(value = "/")     // 通过这里配置使下面的映射都在/下
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 处理"/getUserList/"的GET请求，用来获取用户列表
     *
     * @return
     */
    @GetMapping("/getUserList")
    @ApiOperation(value = "获取用户列表")
    public R getUserList() {
        List<User> user = userService.queryAll();
        return R.right(user,"成功");
    }

    /**
     * 处理"/postUser/"的POST请求，用来创建User
     *
     * @param user
     * @return
     */
    @PostMapping("/postUser")
    @ApiOperation(value = "创建用户", notes = "根据User对象创建用户")
    public R postUser(@RequestBody User user) {
        try {
        User existUser = userService.findUserByName(user.getUsername());
        if(existUser != null){
            //如果用户名已存在
              return   R.error(202,"用户名已存在");
        }
        else{
                userService.queryInsert(user);
                System.out.println(user.getId());
           return R.ok(201,"註冊成功",user);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error(R.FAILED,"發生異常");
        }

    }


    /**
     * 处理"/getUser/{id}"的GET请求，用来获取url中id值的User信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getUser/{id}")
    @ApiOperation(value = "获取用户详细信息", notes = "根据url的id来获取用户详细信息")
    public R getUser(@PathVariable("id") Integer id) {
        List<User> userList = userService.queryAll();
        for(User user: userList){
            if(user.getId().equals(id))
                return  R.right(user, "成功");
        }
        return R.error(R.FAILED,"沒有該用戶");
    }

    /**
     * 处理"/putUser/"的PUT请求，用来更新User信息
     * @param user
     * @return
     */
    @PutMapping("/putUser/")
    @ApiOperation(value = "更新用户详细信息", notes = "根据传过来的user信息中的id来更新用户详细信息")
    public R  putUser( @RequestBody User user) {
        List<User> userList = userService.queryAll();
        if (user != null) {
            for (int i = 0; i < userList.size(); i++) {
                if (userList.get(i).getId().equals(user.getId())) {
                     userService.putUser(user);
                    return R.right(user, "成功");
                }
                return R.error(R.FAILED, "沒有該用戶");
            }
        }
        return R.error(R.FAILED, "請正確輸入");
    }


    /**
     * 处理"/deleteUser/{id}"的DELETE请求，用来删除User
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteUser/{id}")
    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除对象")
    public R deleteUser(@PathVariable Integer id) {
        List<User> userList = userService.queryAll();
        for(User user: userList){
                if (user.getId().equals(id)) {
                    userService.deleteUser(id);
                    return R.right(null, "成功");
                }
        }
        return R.error(R.FAILED,"列表未有該用戶");
    }
}